﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WIKID.Common;
using WIKID.Model;
using WIKID.Controller;


namespace WIKID.View
{
    public partial class AccountView : UserControl
    {
        private AccountModel accountModel = new AccountModel();

        private List<AccountModel> lsAccountModel = new List<AccountModel>();

        private AccountController accountController = new AccountController();

        public AccountView()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Handle load Account View
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void AccountView_Load(object sender, EventArgs e)
        {
            if (!DesignMode)
            {
                
                if (accountController.isAccountsFolderEmpty())
                {
                    listView1.Visible = false;
                    listView2.Visible = false;
                    Point listViewLocation3 = new Point((this.Width - listView3.Width) / 2, (this.Height - listView3.Height) / 2);
                    listView3.Location = listViewLocation3;
                    btnParentCenter.Location = new System.Drawing.Point((listView3.Width - btnParentCenter.Width) / 2, listView3.Height - btnParentCenter.Height - 20);
                    listView3.Controls.Add(btnParentCenter);
                    this.Controls.Add(this.listView3);
                }

                else
                {
                    listView2.Visible = false;
                    listView3.Visible = false;
                    Point listViewLocation = new Point((this.Width - listView1.Width) / 2, (this.Height - listView1.Height) / 2);
                    listView1.Location = listViewLocation;

                    List<Button> accButtons = accountButtons();
                    int count = accButtons.Count;

                    //add list of Account Buttons to ListView
                    for (int i = 0; i < count; i++)
                    {
                        listView1.Controls.Add(accButtons[i]);
                    }
                    this.Controls.Add(listView1);
                }
                btnBack.Visible = false;
            }
        }

        /// <summary>
        /// Create a list of Account Buttons
        /// </summary>
        /// <returns></returns>
        private List<Button> accountButtons()
        {
            lsAccountModel = accountController.getAllAcounts();
            List<Button> btnAccs = new List<Button>();
            for (int i = 0; i < lsAccountModel.Count; i++)
            {
                Button btnAcc = new Button();

                btnAcc.BackColor = Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(0)))), ((int)(((byte)(192)))));
                btnAcc.Font = new Font("Arial Narrow", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                btnAcc.Size = new Size(90, 120);
                btnAcc.TabIndex = 15;
                btnAcc.TextAlign = ContentAlignment.BottomCenter;
                btnAcc.TextImageRelation = TextImageRelation.ImageAboveText;
                btnAcc.UseCompatibleTextRendering = true;
                btnAcc.UseVisualStyleBackColor = false;
                btnAcc.Text = lsAccountModel[i].AccName;
                btnAcc.Tag = lsAccountModel[i].AccId;
                btnAcc.Name = "btnAcc" + (i + 1).ToString();
                btnAcc.BackgroundImage = Image.FromFile(lsAccountModel[i].AccPic);
                btnAcc.BackgroundImageLayout = ImageLayout.Zoom;
                btnAcc.Location = new System.Drawing.Point((listView1.Width - lsAccountModel.Count * 90 - (lsAccountModel.Count-1)*90)/2+183*i, (listView1.Height - 40) / 2);
                btnAcc.Click += new System.EventHandler(this.btnAcc_Click);
                btnAccs.Add(btnAcc);
            }

            return btnAccs;
        }

        //create a list of Password picture boxes
        private List<PictureBox> CreateListPictureBox()
        {
            List<PictureBox> passwordPictureBoxes = new List<PictureBox>();
            int nextRand;
            int picWidth = 100;
            int picDis = 90;
            int picNum = Constants.MAX_RAND + 1;
            int picTop = (listView1.Height - 10) / 2;
            int picLeft = (listView1.Width - picNum * picWidth - Constants.MAX_RAND * picDis) / 2;

            // Create list of Pass ID
            List<int> lsPassId = new List<int>();
            lsPassId.Add(Convert.ToInt32(this.accountModel.PassId));
            Random rand = new Random();
            for (int i = 0; i < Constants.MAX_RAND; i++)
            {
                nextRand = rand.Next(1, 26);
                if (!lsPassId.Contains(nextRand))
                {
                    lsPassId.Add(nextRand);
                }
                else
                {
                    i--; 
                }
            }

            lsPassId = Randomize<int>(lsPassId);

            // Generate Password Picture base on list of Pass ID
            int j = 0;
            foreach (int pass in lsPassId)
            {
                PictureBox pictureBox = new PictureBox();
                if (pass < 10)
                {
                    pictureBox.Image = Image.FromFile(Constants.PASSWORD_IMAGES_FOLDER + "0" + pass.ToString() + ".png");
                }
                else
                {
                    pictureBox.Image = Image.FromFile(Constants.PASSWORD_IMAGES_FOLDER + pass.ToString() + ".png");
                }
                pictureBox.Image.Tag = pass;
                pictureBox.Location = new System.Drawing.Point(picLeft + 183 * j, picTop); ;
                pictureBox.Name = "pictureBox" + (j + 1).ToString();
                pictureBox.Size = new System.Drawing.Size(picWidth, picWidth);
                pictureBox.TabIndex = 9;
                pictureBox.BackColor = Color.Transparent;
                pictureBox.TabStop = false;
                pictureBox.Click += new System.EventHandler(this.pictureBox_Click);

                passwordPictureBoxes.Add(pictureBox);
                j++;
            }

            return passwordPictureBoxes;
        }

        //set visibility for Account Buttons
        private void setVisibleForButtons(bool visibility)
        {
            foreach (Button button in accountButtons())
            {
                button.Visible = visibility;
            }
        }

        private void listView1_SelectedIndexChanged_1(object sender, EventArgs e)
        {

        }

        private void btnAcc_Click(object sender, EventArgs e) 
        {
            string accName = ((Button)sender).Text;
            string accId = ((Button)sender).Tag.ToString();
            foreach (AccountModel tempAccount in lsAccountModel)
            {
                if (accId.Equals(tempAccount.AccId))
                {
                    this.accountModel = tempAccount;
                }
            }
            listView1.Visible = false;
            listView2.Visible = true;
            Point listViewLocation = new Point((this.Width - listView1.Width) / 2, (this.Height - listView1.Height) / 2);
            listView2.Location = listViewLocation;
            List<PictureBox> listOfPictureBoxes = CreateListPictureBox();

            //add list of Account Buttons to ListView
            for (int i = 0; i < listOfPictureBoxes.Count; i++)
            {
                listView2.Controls.Add(listOfPictureBoxes[i]);
            }
                
            this.Controls.Add(this.listView2);
            btnBack.Visible = true;
        }

        private void pictureBox_Click(object sender, EventArgs e)
        {
            String passId = ((PictureBox)sender).Image.Tag.ToString();
            if (accountController.CheckLoginByAccAndPass(this.accountModel, passId))
            {
                frmLogin.current.Visible = false;
                listView2.Visible = false;
                frmMain mainForm = new frmMain();
                mainForm.accountModel = this.accountModel;
                mainForm.Show();
                //frmLogin.current.Close();
            }
            else
            {
                MessageBox.Show("Your password is wrong!");
            }
        }

        private void bntParent_Click(object sender, EventArgs e)
        {
            
            frmParentLogin parentLogin = new frmParentLogin();

            frmParent parentForm = new frmParent();

            this.Parent.Visible = false;

            parentForm.Show();
            parentForm.Enabled = false;
            parentLogin.Show();
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private static List<T> Randomize<T>(List<T> list)
        {
            List<T> randomizedList = new List<T>();
            Random rnd = new Random();
            while (list.Count > 0)
            {
                // Pick a random item from the master list
                int index = rnd.Next(0, list.Count);
                // Place it at the end of the randomized list
                randomizedList.Add(list[index]);
                list.RemoveAt(index);
            }
            return randomizedList;
        }

        private void btnBack_Click(object sender, EventArgs e)
        {
            btnBack.Visible = false;
            listView2.Visible = false;
            listView2.Controls.Clear();
            listView1.Visible = true;
        }
    }
}
